//
//  ios_Label.mm
//  Prism
//
//  Created by Bas du Pre on 24-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "global.h"
#if PRISM_OS_IOS
#include "ios_global.h"

#include "Label.h"
#include "Director.h"
#include "ios_BitMapFontManager.h"

using namespace Prism;

void Label::native_init() {
    native_TTFInit = native_BitMapInit = false;
    native_rerender = true;
}

void Label::native_loadLabel() {
    onFontChange += delegate(this, &Prism::Label::native_onFontChange);
    onSizeChange += delegate(this, &Prism::Label::native_onSizeChange);
    onValueChange += delegate(this, &Prism::Label::native_onValueChange);
    onColorChange += delegate(this, &Prism::Label::native_onColorChange);
    onUpdate += delegate(this, &Prism::Label::native_onUpdate);
    
}

void Label::native_onFontChange(void* sender, EventArgs* e) {
    native_rerender = true;
}

void Label::native_onSizeChange(void* sender, EventArgs* e) {
    native_rerender = true;
}

void Label::native_onValueChange(void* sender, EventArgs* e) {
    if (native_rerender) return;
    if (quality() == QualityHigh) {
        if (!native_TTFInit) {
            native_rerender = true;
        } else {
            [IOS_labelTTF setString:[NSString stringWithUTF8String: value()]];
        }
    } else {
        if (!native_BitMapInit) {
            native_rerender = true;
        } else {
            [IOS_labelBitMap setString:[NSString stringWithUTF8String: value()]];
        }
    }
}

void Label::native_onQualityChange(void* sender, EventArgs* e) {
    native_rerender = true;
}

void Label::native_onColorChange(void* sender, EventArgs* e) {
    if (native_rerender) return;
    if (quality() == QualityHigh) {
        if (!native_TTFInit) {
            native_rerender = true;
        } else {
            [IOS_labelTTF setColor:ccc3(_color.red(), _color.green(), _color.blue())];
            [IOS_labelTTF setOpacity:_color.alpha()];
        }
    } else {
        if (!native_BitMapInit) {
            native_rerender = true;
        } else {
            [IOS_labelBitMap setColor:ccc3(_color.red(), _color.green(), _color.blue())];
            [IOS_labelBitMap setOpacity:_color.alpha()];
        }
    }
}

void Label::native_onUpdate(void* sender, UpdateEventArgs* e) {
    if (!native_rerender)
        return;
    
    if (quality() == QualityHigh) {
        IOS_labelTTF = [CCLabelTTF labelWithString:[NSString stringWithUTF8String: value()]
                                          fontName:[NSString stringWithUTF8String: font()] fontSize:size()];
        [IOS_labelTTF setVisible:YES];
        if (native_BitMapInit) 
            [IOS_labelBitMap setVisible:NO];
        native_scale = 1; // contentScaleFactor does not apply to CCLabelTTF
        native_TTFInit = YES;
        native_setCCNode(IOS_labelTTF);
    } else {
        IOS_BitMapFontManager::BitMapFontSettings settings = bitMapFontManager()->bitMapFontForFontAndSize(font(), 
                                                    [[CCDirector sharedDirector] contentScaleFactor] * size());
        IOS_labelBitMap = [CCLabelBMFont labelWithString:[NSString stringWithUTF8String: value()] fntFile:
                           [NSString stringWithUTF8String:settings.filename]];
        native_scale = settings.scale;
        [IOS_labelBitMap setVisible:YES];
        if (native_TTFInit) [IOS_labelTTF setVisible:NO];
        native_BitMapInit = YES;
        native_setCCNode(IOS_labelBitMap);
    }
    
    native_rerender = false;
    
    // set color
    native_onColorChange(sender, e);
}

Prism::Rect Label::native_boundingBox() {
    Prism::Rect rect;
    CGRect ccRect; 
    if (quality() == QualityHigh) {
        ccRect = [IOS_labelTTF boundingBox];
    } else {
        ccRect = [IOS_labelBitMap boundingBox];
    }
    
    rect.origin.x = ccRect.origin.x;
    rect.origin.y = ccRect.origin.y;
    rect.size.width = ccRect.size.width;
    rect.size.height = ccRect.size.height;
    
    rect.origin = native_correctPosition(rect.origin);
    rect.origin.y -= rect.size.height;
    
    return rect;
}

#endif
